package com.cpi.cornpp.distributionfutureversion.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.distributionfutureversion.dto.DistributionFutureVersionDTO;
import com.cpi.cornpp.distributionfutureversion.exception.DistributionFutureVersionException;
import com.cpi.cornpp.distributionfutureversion.view.DistributionFutureVersionView;
import com.cs.common.utils.NumberUtil;
import com.cs.common.utils.StringUtils;

public class DistributionFutureVersionConverter {

		
		/**
		 * Balance Version DTO to Views Converter
		 * @author Edgar Joao
		 * @param dto
		 * @return
		 * @throws DistributionFutureVersionException
		 */
		public DistributionFutureVersionView converterBalanceVersionDtoToView(DistributionFutureVersionDTO dto, double contractUnit) throws DistributionFutureVersionException{
			DistributionFutureVersionView distributionFutureVersionView = null;
			int futRequired = 0;
			int futAssigned = 0;
			double tonTotal = 0;
			double tonAssigned = 0;
			int futActual = 0;
			try{
				if(dto != null){
					distributionFutureVersionView = new DistributionFutureVersionView();
					distributionFutureVersionView.setFuturePeriodId(String.valueOf(dto.getFuturePeriodId()));
					distributionFutureVersionView.setPeriodDescription(dto.getPeriodDescription());
					distributionFutureVersionView.setPurchaseOrderNum(NumberUtil.convertInteger(dto.getPurchaseOrderNum().intValue()));
					
					if(dto.getFutAssigned()!=null ){
						tonAssigned = dto.getFutAssigned().intValue()/contractUnit;
						distributionFutureVersionView.setTonsAssigned(NumberUtil.convertTon(tonAssigned));
					}else{
						distributionFutureVersionView.setTonsAssigned(StringUtils.EMPTY_STRING);
					}
					if( dto.getTonsTotal()!=null ){
						tonTotal = dto.getTonsTotal().doubleValue();
						distributionFutureVersionView.setTonsTotal(NumberUtil.convertTon(tonTotal));
					}else{
						distributionFutureVersionView.setTonsTotal(StringUtils.EMPTY_STRING);
					}
					if( tonTotal-tonAssigned < 0 ){
						distributionFutureVersionView.setTonsAboutToAssign(NumberUtil.convertTon(0));
					}else{
						distributionFutureVersionView.setTonsAboutToAssign(NumberUtil.convertTon(tonTotal-tonAssigned));
					}
					if( dto.getTonsTotal()!=null ){
						futRequired = (int)((dto.getTonsTotal().doubleValue()*contractUnit)+.99);
						distributionFutureVersionView.setFutRequired(NumberUtil.convertInteger(futRequired));
					}else{
						distributionFutureVersionView.setFutRequired(StringUtils.EMPTY_STRING);
					}
					distributionFutureVersionView.setFutActual(dto.getFutActual()!=null?NumberUtil.convertInteger(dto.getFutActual().intValue()):StringUtils.EMPTY_STRING);
					distributionFutureVersionView.setFutAssigned(dto.getFutAssigned() !=null?NumberUtil.convertInteger(dto.getFutAssigned().intValue()):StringUtils.EMPTY_STRING);
					if( dto.getFutActual()!=null ){
						futActual = dto.getFutActual().intValue();
					}
					if(dto.getFutAssigned()!=null){
						futAssigned = dto.getFutAssigned().intValue();
					}
					distributionFutureVersionView.setFutByAssign(NumberUtil.convertInteger(futActual-futAssigned));
					distributionFutureVersionView.setFutByRetrieve(NumberUtil.convertInteger(futRequired-futAssigned));
				}
			}catch( Exception exception ){
				DistributionFutureVersionException distributionFutureVersionException=null;
				exception.printStackTrace(System.out);
				distributionFutureVersionException= new DistributionFutureVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
				throw distributionFutureVersionException;
			}
			return distributionFutureVersionView;
		}
		/**
		 * @author Edgar Joao
		 * @param balansVersionsDTOs
		 * @return
		 * @throws DistributionFutureVersionException
		 */
		public List convertDTOsToViews(List distributionFutureVersionDTOs, double contractUnit) throws DistributionFutureVersionException{
			List listOfDistributionFutureVersionsDTOs = null;
			Iterator iterator = null;
			DistributionFutureVersionDTO distributionFutureVersionDTO = null;
			DistributionFutureVersionView distributionFutureVersionView = null;
			try{
				listOfDistributionFutureVersionsDTOs = new ArrayList(0);
				iterator = distributionFutureVersionDTOs.iterator();
				while( iterator.hasNext() ){
					distributionFutureVersionDTO =  (DistributionFutureVersionDTO) iterator.next();
					distributionFutureVersionView = converterBalanceVersionDtoToView(distributionFutureVersionDTO, contractUnit);
					listOfDistributionFutureVersionsDTOs.add(distributionFutureVersionView);
				}						
			}catch(DistributionFutureVersionException distributionFutureVersionException){
				throw distributionFutureVersionException;
			}catch( Exception exception ){
				DistributionFutureVersionException distributionFutureVersionException=null;
				exception.printStackTrace(System.out);
				distributionFutureVersionException= new DistributionFutureVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
				throw distributionFutureVersionException;
			}
			return listOfDistributionFutureVersionsDTOs;
		}
	}


